package com.itboye.lottery.activity

import android.annotation.SuppressLint
import android.content.Intent
import android.net.Uri
import android.support.v7.app.AlertDialog
import android.util.Log
import android.view.View
import android.webkit.*
import android.widget.ProgressBar
import android.widget.RelativeLayout
import com.itboye.lottery.R
import com.itboye.lottery.base.BaseActivity
import kotlinx.android.synthetic.main.activity_web.*

class WebActivity : BaseActivity() {

    var lay_actionbar_left: RelativeLayout? = null
    var web_progress: ProgressBar? = null
    override fun onClick(v: View?) {
        when (v) {
            lay_actionbar_left -> {
                finish()
            }
            else -> {
            }
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_web
    }

    var webSettings: WebSettings? = null
    @SuppressLint("JavascriptInterface")
    override fun initData() {
        setTitle(intent.getStringExtra("title"))
        webView.loadUrl(intent.getStringExtra("url"))
        Log.v("web_url", intent.getStringExtra("url"))
        webSettings = webView.settings
        //设置支持JavaScript
        webSettings?.loadWithOverviewMode = true
        webSettings?.javaScriptEnabled = true
        //设置不调用浏览器，使用本WebView
        webView.webViewClient = object : WebViewClient() {
            override fun onPageFinished(view: WebView?, url: String?) {
                super.onPageFinished(view, url)
            }

            override fun onReceivedError(view: WebView?, request: WebResourceRequest?, error: WebResourceError?) {
//                MAlert.alert("当前无网络连接")
                super.onReceivedError(view, request, error)
            }

            override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
                if (url!!.startsWith("alipays:") || url.startsWith("alipay")) {
                    try {
                        startActivity(Intent("android.intent.action.VIEW",
                                Uri.parse(url)))
                    } catch (e: Exception) {
                        AlertDialog.Builder(this@WebActivity)
                                .setMessage("未检测到未安装支付宝客户端，请安装后重试。")
//                                .setPositiveButton("⽴立即安装",object :{
//                                    override fun onClick(dialog: DialogInterface, which: Int) {
//                                        var alipayUrl = Uri.parse("https://d.alipay.com")
//                                        startActivity(Intent ("android.intent.action.VIEW", alipayUrl))
//                                    }
//                                })
                                .setNegativeButton("取消", null)
                                .show()
                    }
                    return true
                }
// ------- 处理理结束 -------
                if (!(url.startsWith("http") || url.startsWith("https"))) {
                    return true
                }
                view!!.loadUrl(url)
                return true
            }

//            override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
//
//                    return true
//            }
        }
        webView.webChromeClient = object : WebChromeClient() {
            override fun onProgressChanged(view: WebView, newProgress: Int) {
                super.onProgressChanged(view, newProgress)
                if (newProgress == 100) {
                    web_progress?.visibility = View.GONE
                    //progressBar.setProgress(newProgress);
                } else {
                    web_progress?.visibility = View.VISIBLE
                    web_progress?.progress = newProgress//设置加载进度
                }
            }
        }
    }
}
